import scrapy


class LoginMoniSpider(scrapy.Spider):
    name = "login_moni"


    async def start(self):
        yield scrapy.Request(url="https://quotes.toscrape.com/login")

    def parse(self, response):
        print(response.headers)

        from scrapy.mail import MailSender

        # try:
        #     # ms = MailSender( smtpport=25, smtphost="smtp.163.com", mailfrom="15138001200@163.com",
        #     #                 smtpuser="15138001200@163.com", smtppass="FXbBEgNVrRH6yJju")
        #
        #     ms = MailSender.from_crawler(self.crawler)
        #     ms.send(["496575233@qq.com"], subject="scrapy", body="scrapy测试文件")
        #
        #     print(f"发送完毕")
        # except Exception as e:
        #     print("eeee", e)


        csrf_token = response.xpath('//input[@name="csrf_token"]/@value').get()
        yield scrapy.FormRequest(url="https://quotes.toscrape.com/login", formdata={
            "username": "admin",
            "password": "123456",
            "csrf_token": csrf_token
        }, callback=self.parse2)

    def parse2(self, response):
        print(response.status)
